# -*- coding: utf-8 -*-
"""
Code to create the bill-level data for analysis in the Deaths of Ideas in Congress

First created: 5/1/23
Final edit: 12/18/23

"""

import pandas as pd
import numpy as np

bill_dv = pd.DataFrame()
bill_dv2 = pd.DataFrame()
full_data=pd.read_csv('/replication/deathideas_data.csv')
full_data = full_data.drop_duplicates(subset=['section']) #Remove any duplicate sections

#Keep key section level variables but sum them for inclusion later on
bill_dv = full_data.groupby('billid')[('death', 'reintro_identical', 'reintro_near_identical', 'momentum', 'companion', 'tariff', 'post_office', 'approps', 'gold_medal', 'coins')].sum().reset_index()

#Create bill section count variable
bill_dv2 = full_data.groupby('billid').count().reset_index()
bill_dv2 = bill_dv2[['billid', 'section']]
bill_dv2=bill_dv2.rename(columns={"section":"bill_section_count"})
bill_dv = pd.merge(bill_dv, bill_dv2, how="left", on="billid")
bill_dv['perc_died'] = bill_dv['death']/bill_dv['bill_section_count'] #Create % died dependent variable

bill_dv['all_died'] = np.where(bill_dv["perc_died"] < 1, 0, 1) #Create all died DV
bill_dv['momentum'] = np.where(bill_dv["momentum"] > 0, 1, 0) #Create momentum dummy variable
bill_dv['companion'] = np.where(bill_dv["companion"] > 0, 1, 0) #Create companion dummy variable

#Drop section level data not used in bill-level analyses
full_data.drop(['death', 'reintro_identical', 'reintro_match', 'reintroduced', 'momentum', 'companion', 'bill_section_count', 'reintro_near_identical',  'tariff', 'post_office', 'approps', 'gold_medal', 'coins', 'version', 'actiondatex', 'congress', 'enacted_match', 'cong', 'type', 'billnum' ], axis=1, inplace=True)

#Merge bill and section-level data to get member-specific covariates
bill_dv = pd.merge(bill_dv, full_data, how="inner", on="billid")

#Drop section specific variables not used in this analysis
bill_dv.drop(['sec_complexity', 'section', 'bill', 'date', 'dollars'], axis=1, inplace=True)

bill_dv_final = bill_dv
bill_dv_final = bill_dv_final.groupby('billid').head(1)
bill_dv_final.to_csv(r'\replication\deathideas_billdata.csv')

